package cn.krui.project.service.system;

import cn.krui.helper.db.DBHelper;
import cn.krui.helper.db.SQL;
import cn.krui.project.bean.system.sys_params.sys_params;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: project-system
 * @description: 系统配置模块
 * @author: zc
 * @create: 2019-10-11 10:57
 **/
@Service
public class SysParamsService {

	@Autowired
	private DBHelper dbHelper;

	/**
	 * 系统模块查询
	 */
	public List<sys_params> selectSysPaList(sys_params sysParams) {
		SQL.SelectSQL sql = SQL.select("*").from("qr_sys_params").where("init_default=0 and pid=0");
		if (!StringUtils.isEmpty(sysParams.getPid())) {
			sql.where("pid=" + sysParams.getPid());
		}
		if (!StringUtils.isEmpty(sysParams.getClass_name())) {
			sql.where("class_name like '%" + sysParams.getClass_name() + "%'");
		}
		if (!StringUtils.isEmpty(sysParams.getName())) {
			sql.where("name like '%" + sysParams.getName() + "%'");
		}
		if (!StringUtils.isEmpty(sysParams.getNumber_value())) {
			sql.where("number_value=" + sysParams.getNumber_value());
		}
		if (!StringUtils.isEmpty(sysParams.getDate_value())) {
			sql.where("date_value=" + sysParams.getDate_value());
		}
		if (!StringUtils.isEmpty(sysParams.getTime_value())) {
			sql.where("time_value=" + sysParams.getTime_value());
		}
		if (!StringUtils.isEmpty(sysParams.getString_value())) {
			sql.where("string_value like '%" + sysParams.getString_value() + "'%");
		}
		if (!StringUtils.isEmpty(sysParams.getRemark())) {
			sql.where("remark=" + sysParams.getRemark());
		}
		// sql.page(Integer.parseInt(page),Integer.parseInt(size));
		List<sys_params> sysParamsList = dbHelper.select(sql, sys_params.class);
		return sysParamsList;
	}

	/**
	 * 添加系统模块
	 * 
	 * @param sysParams
	 * @return
	 */
	public Integer addSysParams(sys_params sysParams) {
		Object i = dbHelper.insert("qr_sys_params", sysParams, sys_params.class);
		return Integer.valueOf(i.toString());
	}

	/**
	 * 更新系统模块
	 * 
	 * @return
	 */
	public Integer updateSysParams(sys_params sysParams) {
		Map<String, Object> addr = new HashMap<>(10);
		if (!StringUtils.isEmpty(sysParams.getPid())) {
			addr.put("pid", sysParams.getPid());
		}
		if (!StringUtils.isEmpty(sysParams.getClass_name())) {
			addr.put("class_name", sysParams.getClass_name());
		}
		if (!StringUtils.isEmpty(sysParams.getName())) {
			addr.put("name", sysParams.getName());
		}
		if (!StringUtils.isEmpty(sysParams.getNumber_value())) {
			addr.put("number_value", sysParams.getNumber_value());
		}
		if (sysParams.getDate_value()!=null) {
			addr.put("date_value", sysParams.getDate_value());
		}
		if (sysParams.getTime_value()!=null) {
			addr.put("time_value", sysParams.getTime_value());
		}
		if (!StringUtils.isEmpty(sysParams.getString_value())) {
			addr.put("string_value", sysParams.getString_value());
		}
		if (!StringUtils.isEmpty(sysParams.getRemark())) {
			addr.put("remark", sysParams.getRemark());
		}
		Map<String, Object> where_params = new HashMap<String, Object>(1);
		where_params.put("id", sysParams.getId());
		Integer i = dbHelper.update("qr_sys_params", addr, "id=#{id} ", where_params);
		return i;
	}

	/**
	 * 删除系统模块
	 * 
	 * @return
	 */
	public Integer deleteSysParams(sys_params sysParams) {
		SQL.DeleteSQL sql = SQL.delete("qr_sys_params").where("id=#{id}").addParameter("id", sysParams.getId());
		int i = dbHelper.delete(sql);
		return i;
	}

}
